﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.Album
{
    public partial class PhotoAdd : System.Web.UI.Page
    {
        private int userID ;//暂用用户ID，写死，到时候用COOKIE替换过来
        protected void Page_Load(object sender, EventArgs e)
        {
            string msg = string.Empty;
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string createAlbum = Request.Form["submit"];
            if (Request.Form["submit"] == "创建相册上传相片")
            {
                Jy123.Model.User_Album mAlbum = new Jy123.Model.User_Album();
                Jy123.BLL.User_Album bllAlbum = new Jy123.BLL.User_Album();
                if (string.IsNullOrEmpty(Request.Form["albumTitle"])
                    || string.IsNullOrEmpty(Request.Form["albumDescription"]))
                {
                    msg = "<script>$(function(){$.moonbox.error('有信息未填写!');})</script>";
                }
                else
                {
                    mAlbum.FUser = userID;
                    mAlbum.Title = Request.Form["albumTitle"];
                    mAlbum.Description = Request.Form["albumDescription"];
                    mAlbum.Privacy = int.Parse(Request.Form["selPrivacy"]);
                    mAlbum.ImagesCount = 0;
                    mAlbum.CreateTime = DateTime.Now;
                    mAlbum.LastUploadTime = DateTime.Now;
                    mAlbum.GroupID = 0;
                    int newAlbumID = bllAlbum.Add(mAlbum);

                    if (newAlbumID > 0)
                    {
                        //加入动态
                        Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
                        Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();
                        mUserForState = bllUserForState.GetModel(userID);

                        Jy123.Model.User_Album mAlbumForState = new Jy123.Model.User_Album();
                        Jy123.BLL.User_Album bllAlbumForState = new Jy123.BLL.User_Album();
                        mAlbumForState = bllAlbumForState.GetModel(newAlbumID);
                        new Jy123.BLL.Public.State().CreateAlbumState(mUserForState, mAlbumForState, (int)Jy123.BLL.Public.EnumConfig.StateActionType.CreateAlbum);
                        //加入动态

                        Response.Redirect("PhotoUpload.aspx?AlbumID=" + newAlbumID);
                    } 
                }                
            }
            else if (Request.Form["submit"] == "选择相册上传相片")
            {
                string albumID = Request.Form["selAlbum"];
                if (!string.IsNullOrEmpty(albumID))
                {
                    Response.Redirect("PhotoUpload.aspx?AlbumID=" + albumID);
                } 
            }
            #region 公共部分
            StringBuilder strHtml = new StringBuilder(Cache["user_photoadd"].ToString());//取得主HTML页面
            strHtml = strHtml.Replace("$Header$", Cache["user_header2"].ToString());
            strHtml = strHtml.Replace("$Footer$", Cache["user_footer"].ToString());
            strHtml = strHtml.Replace("$LeftUserCmd$", Cache["user_navigation_down"].ToString());
            strHtml = strHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());

            //统计站内信数量
            Jy123.BLL.User.Letter bllLetter = new Jy123.BLL.User.Letter();
            int newLetterCount = bllLetter.CountNewLetter(userID);
            strHtml = strHtml.Replace("$NewMsg$", newLetterCount.ToString());
            #endregion

            //选择相册下拉表
            StringBuilder sbAlbumSelect = new StringBuilder();
            string strAlbumSelect = Cache["user_albumselect"].ToString();
            DataTable dtAlbumSelect = new Jy123.BLL.User_Album().GetList("FUser = '"+userID+"'").Tables[0];
            for (int i=0; i < dtAlbumSelect.Rows.Count; i++)
            {
                string strOption = strAlbumSelect;
                strOption = strOption.Replace("$AlbumID$", dtAlbumSelect.Rows[i]["ID"].ToString())
                    .Replace("$Title$",dtAlbumSelect.Rows[i]["Title"].ToString());

                sbAlbumSelect.Append(strOption);                
            }
            strHtml = strHtml.Replace("$AlbumSelectOption$", sbAlbumSelect.ToString());


            //写出整个页面
            Response.Write(strHtml.ToString()+msg);
        }
    }
}
